<html>
<head><meta charset="utf-8"><title>visibility inlay hint · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html">visibility inlay hint</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216018842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216018842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216018842">(Nov 08 2020 at 18:26)</a>:</h4>
<p>(deleted)</p>



<a name="216019006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216019006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216019006">(Nov 08 2020 at 18:31)</a>:</h4>
<p>One of things we do in clap is to put <code>pub(crate)</code> on every private thing. Let me explain why. Assume we have <code>foo.rs</code></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">baz</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>We want to expose only <code>baz</code> to end users but still use <code>bar</code> internally in other modules:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">foo</span><span class="p">;</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">foo</span>::<span class="n">baz</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">uses_bar</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span>::<span class="n">bar</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>In that situation, when we are working on <code>foo.rs</code>, we have no hint about the visibility of the items. Are they visibile to public? Are they visibile to only <code>super</code>? etc.. So, to make it easier for us, we ended up doing <code>pub(crate)</code> on all internal things which we want to use elsewhere in the codebase.</p>
<p>I think this is one of things LSP can do. Provide an inlay hint that can display the visibility reach of each item that has <code>pub</code>.</p>



<a name="216019227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216019227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216019227">(Nov 08 2020 at 18:36)</a>:</h4>
<p>if I understand you correctly, you might be interested in the <a href="https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unreachable-pub">unreachable-pub lint</a></p>



<a name="216019300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216019300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216019300">(Nov 08 2020 at 18:38)</a>:</h4>
<p>Yeah, we do have that. But I just don't like it. I think the IDE should display the info of the restriction level of visibility</p>



<a name="216019315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216019315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216019315">(Nov 08 2020 at 18:38)</a>:</h4>
<p>Instead of forcing all my contributors to add <code>pub(crate)</code></p>



<a name="216019863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216019863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216019863">(Nov 08 2020 at 18:52)</a>:</h4>
<p>If you use <code>pub</code> instead of <code>pub(crate)</code> rustc will for all intents and purposes consider it exported. The private-in-public lint won't fire, the missing_docs lint will fire and so on.</p>



<a name="216061173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216061173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216061173">(Nov 09 2020 at 09:42)</a>:</h4>
<p>I do think this is a language-level concern, and that the fix is indeed to use <code>pub</code> only for exported items, and roll with <code>-Dunreacheable_pub</code> otherwise.</p>



<a name="216061201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216061201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216061201">(Nov 09 2020 at 09:42)</a>:</h4>
<p>It is hugely helpful to see <code>pub</code> and immediately know "hey, this is a public API"</p>



<a name="216061260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216061260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216061260">(Nov 09 2020 at 09:43)</a>:</h4>
<p>We recently begin enforcing this on CI: <a href="https://github.com/rust-analyzer/rust-analyzer/commit/ba8d6d1e4ea2590b31470171efc175b0301c5e1c">https://github.com/rust-analyzer/rust-analyzer/commit/ba8d6d1e4ea2590b31470171efc175b0301c5e1c</a></p>



<a name="216093302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216093302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216093302">(Nov 09 2020 at 14:46)</a>:</h4>
<p>Thanks for the discussion guys. I am now thinking that we should get clippy to have that lint by default turned on then</p>



<a name="216093675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216093675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216093675">(Nov 09 2020 at 14:49)</a>:</h4>
<p>Its not the clippy lint, it's compiler lint. </p>
<p>The plan was to toggle it in 2018, but that didn't happen, in large part due to <code>crate</code> visibility modifier not happening</p>



<a name="216100710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216100710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216100710">(Nov 09 2020 at 15:37)</a>:</h4>
<p>unreachable_pub is buggy though :/</p>



<a name="216100934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216100934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216100934">(Nov 09 2020 at 15:39)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/64762">https://github.com/rust-lang/rust/issues/64762</a></p>



<a name="216100944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216100944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216100944">(Nov 09 2020 at 15:39)</a>:</h4>
<p>Yeah, I've noticed that it doesn't hanle this case</p>
<div class="codehilite"><pre><span></span><code>pub use m::foo;
mod m { pub fn fo0() {}}
</code></pre></div>



<a name="216101003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216101003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216101003">(Nov 09 2020 at 15:39)</a>:</h4>
<p>Oh I'm ok with false negatives, it also has false positives</p>



<a name="216101016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216101016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216101016">(Nov 09 2020 at 15:39)</a>:</h4>
<p>And those are really annoying</p>



<a name="216101043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216101043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216101043">(Nov 09 2020 at 15:39)</a>:</h4>
<p>I mean, that's false posive</p>



<a name="216101118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216101118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216101118">(Nov 09 2020 at 15:40)</a>:</h4>
<p>And, looking at the issue, I think that actually needs to be <code>m::{foo, bar}</code>.</p>



<a name="216203051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216203051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216203051">(Nov 10 2020 at 12:19)</a>:</h4>
<p>Hmm.. I guess I will wait for 2021 edition roadmap and then I can propose turning this to <code>warn</code></p>



<a name="216203091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216203091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216203091">(Nov 10 2020 at 12:19)</a>:</h4>
<p>you don't need an edition break to make lints warn-by-default</p>



<a name="216203116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216203116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216203116">(Nov 10 2020 at 12:19)</a>:</h4>
<p>you do however need to make sure there aren't false positives</p>



<a name="216203131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/visibility%20inlay%20hint/near/216203131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/visibility.20inlay.20hint.html#216203131">(Nov 10 2020 at 12:19)</a>:</h4>
<p>so <a href="https://github.com/rust-lang/rust/issues/64762">https://github.com/rust-lang/rust/issues/64762</a> needs to be fixed first</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>